package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fachlogik.Item;
import fachlogik.Eintrag;
import fachlogik.LagerException;
public class Hauptfenster extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Item item;
	private JPanel contentPane;
	private JList<Eintrag> eintragsliste;
	private EintragEditor eintrageditor;
	private DefaultListModel<Eintrag> listModel = new DefaultListModel<Eintrag>();
	private JTextField tfsearch;
	
	
	public Hauptfenster(Item item)
	{
		this.item = item;
		initFrame();
		eintrageditor.setEnabled(false);
		try
		{
			updateList();
		}catch (LagerException e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	public void updateList() throws LagerException
	{
		listModel.clear();
		for (Eintrag e: item.suchen(tfsearch.getText()))
			listModel.addElement(e);
	}
	
	private void initFrame()
	{
		setTitle("Lagerverwaltung");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(10, 10, 800, 500);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnItem = new JMenu("Items");
		menuBar.add(mnItem);
		
		JMenuItem mntmLschen = new JMenuItem("l\u00F6schen");
		mntmLschen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_mntmLschen_actionPerformed(arg0);
			}
		});
		mnItem.add(mntmLschen);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5,5,5,5));
		contentPane.setLayout(new BorderLayout(0,0));
		setContentPane(contentPane);
		
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.2);
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		JPanel left = new JPanel(new BorderLayout());
		splitPane.setLeftComponent(left);
		
		
		JScrollPane scrollPane = new JScrollPane();
		left.add(scrollPane, BorderLayout.CENTER);
		
		eintragsliste = new JList<Eintrag>(listModel);
		eintragsliste.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				do_angebotsListe_valueChanged(arg0);
			}
		});
		scrollPane.setViewportView(eintragsliste);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Suche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		left.add(panel_3, BorderLayout.NORTH);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		tfsearch = new JTextField();
		tfsearch.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				do_tfFilter_keyReleased(arg0);
			}
		});
		
		panel_3.add(tfsearch);
		tfsearch.setColumns(10);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		splitPane.setRightComponent(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Bearbeiten", null, panel, null);
		panel.setLayout(new BorderLayout(0, 0));
		
		eintrageditor = new EintragEditor();
		JScrollPane scrollPane_1 = new JScrollPane(eintrageditor);
		panel.add(scrollPane_1, BorderLayout.CENTER);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.SOUTH);
		
		JButton btnSpeichern = new JButton("Speichern");
		btnSpeichern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnSpeichern_actionPerformed(e);
			}
		});
		panel_2.add(btnSpeichern);
		
		JButton btnNeu = new JButton("Neu");
		btnNeu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnNeu_actionPerformed(arg0);
			}
		});
		panel_2.add(btnNeu);
		
		
	}
	
	
	protected void do_btnSpeichern_actionPerformed(ActionEvent e) 
	{
		try
		{
			Eintrag en = eintrageditor.getEintrag();  // aktualisiert die Werte 
			item.speichern(en);
			updateList();
			eintragsliste.setSelectedValue(en, true); 
			System.out.println(en);
		
		} catch (LagerException ex)
		{
			JOptionPane.showMessageDialog(this, ex.getMessage());
		}
		
	}
	
	protected void do_btnNeu_actionPerformed(ActionEvent arg0) 
	{
		try
		{
			Eintrag en = new Eintrag();
			this.item.speichern(en);
			this.updateList();
			this.eintragsliste.setSelectedValue(en, true);
			System.out.println(en);
		} catch (LagerException e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	
	protected void do_angebotsListe_valueChanged(ListSelectionEvent arg0) 
	{
		
		eintrageditor.setEnabled(eintragsliste.getSelectedValue() != null);
		try
		{
			eintrageditor.getEintrag();  // bewirkt ein Update
			Eintrag e = eintragsliste.getSelectedValue();
			eintrageditor.setEintrag(e);
		} catch (LagerException e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	
	protected void do_mntmLschen_actionPerformed(ActionEvent arg0) 
	{
		Eintrag e = eintragsliste.getSelectedValue();
		if (e != null)
		{
			try
			{
				item.entfernen(e);
				updateList();
			} catch (LagerException ex)
			{
				ex.printStackTrace();
				JOptionPane.showMessageDialog(this, ex.getMessage());
			}
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Bitte vorher ein Angebot ausw�hlen");
		}
	}
	
	
	protected void do_tfFilter_keyReleased(KeyEvent arg0) 
	{
		try
		{
			updateList();
		} catch (LagerException e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	
	
	
}
